
$(document).ready(
    function() {
        var VAL = {
            NORMAL : "0000000",
            T_FIPINQBOX : "FIPINQBOX",
            T_FIPINQSHT : "FIPINQSHT",
            T_FBPMDLDEF : "FBPMDLDEF",
            T_FSPGRDCHG : "FSPGRDCHG",
            T_FBPLSTDAT : "FBPBISDAT",
            EVT_USER :$("#userId").text(),
            DISABLED_ATTR : {
                'disabled' : true
            },
            ENABLED_ATTR : {
                'disabled' : false
            }
        };
        var domObj = {
            $prdIdFk :$("#prdIdFk"),

            $prdSeqId   : $("#prdSeqId"),
            $prdStat    : $("#prdStat"),
            $prdGrade   : $("#prdGrade"),
            $mdlIdFk    : $("#mdlIdFk"),
            $woIdFk     : $("#woIdFk"),
            $crOpeNoFk  : $("#crOpeNoFk"),
            $nxOpeNoFk  : $("#nxOpeNoFk"),
            $nxProcIdFk : $("#nxProcIdFk"),
            $nxPathIdFk : $("#nxPathIdFk"),
            $nxPathVerFk: $("#nxPathVerFk"),
            $crPathIdFk: $("#crPathIdFk"),
            $crPathVerFk: $("#crPathVerFk"),

            $prd_grade: $("#prd_grade"),
            $gradeGRP: $("#gradeGRP"),
            $radioOK: $("#radioOK"),
            $radioNG: $("#radioNG"),
            $mdlId:   $("#mdlId"),
            $defectCode:$("#defectCode"),
            $prdNote:$("#prdNote"),
            buttons : {
                $query_btn : $("#query_btn"),
                $selectReason_btn : $("#selectReason_btn"),
                $deleteReason_btn : $("#deleteReason_btn"),
                $updateLev_btn : $("#updateLev_btn"),
                $select_file : $("#select_file"),
                $file_name : $("#file_name"),
                $save_btn:  $("#save_btn"),
            },
            grids : {
                $reasonListGrd : $("#reasonListGrd"),
                $reasonListDiv : $("#reasonListDiv"),
                $reasonListPg : $("#reasonListPg"),
                $glassListGrd : $("#glassListGrd"),
                $glassListDiv : $("#glassListDiv"),
                $glassListPg : $("#glassListPg")
            },
            dialogs : {
                $addReasonDialog : $("#addReasonDialog"),
                $updateLevDialog : $("#updateLevDialog"),
                $deptId : $("#deptId"),
                $modalReasonListDiv : $("#modalReasonListDiv"),
                $modalReasonListGrd : $("#modalReasonListGrd"),
                $modalReasonListPg : $("#modalReasonListPg"),

                $reModalReasonListDiv : $("#reModalReasonListDiv"),
                $reModalReasonListGrd : $("#reModalReasonListGrd"),
                $reModalReasonListPg : $("#reModalReasonListPg"),

                // 对话框的确认按钮
                $reasonDialog_sureBtn : $("#reasonDialog_sureBtn"),
                $updateLevDialog_sureBtn : $("#updateLevDialog_sureBtn"),
            }
        };
        var initSelectFnc = {
            iniGdGRP: function () {
                var okng = "";
                var mdlId = $.trim(domObj.$mdlIdFk.val());
                if ($("#radioOK").attr("checked") == "checked") {
                    okng = "OK";
                } else {
                    okng = "NG";
                }
                var inObj = {
                    trx_id: VAL.T_FBPMDLDEF,
                    action_flg: "M",
                    iary: [{
                        mdl_id: mdlId,
                        okng: okng
                    }]
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    var oary = outObj.oary;
                    var compare = function (obj1, obj2) {
                        var val1 = obj1.gd_grd;
                        var val2 = obj2.gd_grd;
                        if (val1 < val2) {
                            return -1;
                        } else if (val1 > val2) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                    oary.sort(compare);
                    SelectDom.addSelectArr(domObj.$gradeGRP,oary, "gd_grp", "gd_grp", "", true);
                }
            },
            iniPrdGRP: function () {
                var okng = "";
                var mdlId = $.trim(domObj.$mdlIdFk.val());
                if ($("#radioOK").attr("checked") == "checked") {
                    okng = "OK";
                } else {
                    okng = "NG";
                }
                var iary = {
                    mdl_id: mdlId,
                    okng: okng
                }
                var inObj = {
                    trx_id: VAL.T_FBPMDLDEF,
                    action_flg: "N",
                    iary: [iary]
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    var oary = outObj.oary;
                    var compare = function (obj1, obj2) {
                        var val1 = obj1.prd_grade;
                        var val2 = obj2.prd_grade;
                        if (val1 < val2) {
                            return -1;
                        } else if (val1 > val2) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                    oary.sort(compare);
                    SelectDom.addSelectArr(domObj.$prd_grade,oary, "prd_grade", "prd_grade", "", true);
                }
            },
            inidefectCode: function () {
                comAddValueByDataCateFnc("#defectCode", "DFCT", "data_ext", "data_ext", true);
            }
        }
        var btnFunc = {
            queryBtnfunc: function () {
                clearDataAction();
                var prdIdFk, inObj, outObjs;
                prdIdFk = $.trim(domObj.$prdIdFk.val());
                if ("" == prdIdFk) {
                    showErrorDialog("", "屏体代码不能为空");
                    return false;
                }
                inObj = {
                    trx_id: VAL.T_FIPINQSHT,
                    action_flg: "Q",
                    prd_seq_id: prdIdFk
                };
                outObjs = comTrxSubSendPostJson(inObj);
                if (outObjs.rtn_code === VAL.NORMAL) {
                    if(outObjs.prd_stat=='WAIT'){
                        setGridInfo(outObjs, "#glassListGrd");
                    }else{
                        showErrorDialog("", "屏体["+prdIdFk+"]当前状态为["+outObjs.prd_stat+"]");
                        return false;
                    }

                }
            },
            selectReasonBtnfunc: function () {
                domObj.dialogs.$modalReasonListGrd
                    .jqGrid("clearGridData");
                domObj.dialogs.$reModalReasonListGrd
                    .jqGrid("clearGridData");
                // 原因代码对话框的模型
                domObj.dialogs.$addReasonDialog.modal({
                    backdrop: true,
                    keyboard: false,
                    show: false
                });
                domObj.dialogs.$addReasonDialog.modal("show");

                //初始化部门代码
                comAddValueByDataCateFnc("#deptId", "DEPT", "data_ext", "data_desc", true);
                var deptId, iary, inObj, outObj;
                deptId = domObj.dialogs.$deptId.val();
                iary = {
                    data_cate: "GDRS",
                };
                inObj = {
                    trx_id: VAL.T_FBPLSTDAT,
                    action_flg: "Q",
                    iary: [iary]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    var deptIds = domObj.dialogs.$deptId.val();
                    var oary = [];
                    for (var i = 0; i < outObj.oary.length; i++) {
                        var reason = {};
                        reason.deptId = deptIds;
                        reason.data_id = outObj.oary[i].data_id;
                        reason.data_desc = outObj.oary[i].data_desc;
                        oary.push(reason);
                    }
                    setGridInfo(oary, "#modalReasonListGrd");
                }
            },
            deleteReasonBtnfunc: function () {
                var selRowId = domObj.grids.$reasonListGrd.jqGrid(
                    "getGridParam", "selrow");
                if (selRowId == null || selRowId.length < 1) {
                    showErrorDialog("", "请至少选择一条原因代码进行删除");
                    return false;
                }
                $("#deleteReason_btn").showCallBackWarnningDialog(
                    {
                        errMsg: "是否删选原因代码,请确认!!!!",
                        callbackFn: function (data) {
                            if (data.result == true) {
                                // 动态删除jqgrid行时，数据会在动态变化
                                var length = selRowId.length;
                                for (var i = 0; i < length; i++) {
                                    domObj.grids.$reasonListGrd.jqGrid("delRowData", selRowId[0]);
                                }
                            }
                        }
                    });
            },
            saveBtnfunc :function () {
                var reasonRowData =domObj.grids.$reasonListGrd.jqGrid("getRowData");
                var rowId= domObj.grids.$glassListGrd.jqGrid("getGridParam", "selrow");
                var rowData = domObj.grids.$glassListGrd.jqGrid("getRowData", rowId);
              /*  var rowIds = domObj.grids.$glassListGrd.jqGrid("getGridParam","selarrrow");*/
                var data = new Array();
                    data.push(rowData);
                if(reasonRowData.length<1){
                    showErrorDialog("", "原因代码不能为空");
                    return;
                }
                if(rowData.length<1){
                    showErrorDialog("", "屏体等级不能为空");
                    return;
                }
                prdseqId = domObj.$prdIdFk.val();
                rsnCode = reasonRowData[0].deptId;
                rsnCate = "GDRS";
                rsnDept = reasonRowData[0].data_id;
                iary = {
                    rsn_code       : rsnCode,
                    rsn_cate       : rsnCate,
                    rsn_dept       : rsnDept,
                    evt_usr        : VAL.EVT_USER ,
                    ret_prd_info   : data
                };
                inObj = {
                    trx_id : VAL.T_FSPGRDCHG,
                    action_flg : "U",
                    iary : [iary]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    showSuccessDialog("等级修改成功");
                    btnFunc.queryBtnfunc();//更新信息
                }
            },
            changeOkFnc:function () {
                SelectDom.setSelect($("#defectCode"), "", "");
                $("#defectCode").attr(VAL.DISABLED_ATTR);
                $("#prdNote").attr(VAL.DISABLED_ATTR);
                domObj.$prd_grade.attr(VAL.ENABLED_ATTR);
                domObj.$gradeGRP.attr(VAL.ENABLED_ATTR);
                initSelectFnc.iniGdGRP();
                initSelectFnc.iniPrdGRP();
              },
             changeNgFnc: function(){
                domObj.$prd_grade.attr(VAL.ENABLED_ATTR);
                domObj.$gradeGRP.attr(VAL.ENABLED_ATTR);
                $("#defectCode").attr(VAL.ENABLED_ATTR);
                $("#prdNote").attr(VAL.ENABLED_ATTR);
                 initSelectFnc.iniGdGRP();
                 initSelectFnc.iniPrdGRP();
            },
            updateLevBtnfunc: function () {
                initSelectFnc.iniPrdGRP();
                initSelectFnc.iniGdGRP();
                var rowId = domObj.grids.$glassListGrd
                    .jqGrid("getGridParam", "selrow");
                if (rowId == null || rowId.length < 1) {
                    showErrorDialog("", "请至少选择一条玻璃信息进行修改");
                    return false;
                }
                var reasonRowData = domObj.grids.$reasonListGrd.jqGrid("getRowData");
                if (reasonRowData.length < 1) {
                    showErrorDialog("", "原因代码不能为空");
                    return;
                }
                domObj.dialogs.$updateLevDialog.modal({
                    backdrop: true,
                    keyboard: false,
                    show: false
                });
                domObj.dialogs.$updateLevDialog.modal("show");

            }
        };
        // 主页面grid初始化
        var iniGridInfo = function() {
            domObj.grids.$reasonListGrd.jqGrid({
                url : "",
                datatype : "local",
                mtype : "POST",
                height : 100,
                width : domObj.grids.$reasonListDiv.width(),
                autowidth : true,
                shrinkToFit:false,
                autoScroll: false,
                resizable : true,
                loadonce : true,
                viewrecords : true,
                rownumbers : true,
                rowNum : 20,
                emptyrecords : true,
                multiselect : false,
                pager : domObj.grids.$reasonListPg,
                colModel : [ {
                    name : 'deptId',
                    index : 'deptId',
                    label : '部门代码',
                    width : 280
                }, {
                    name : 'data_id',
                    index : 'data_id',
                    label : '原因代码',
                    width : 280
                }, {
                    name : 'data_desc',
                    index : 'data_desc',
                    label : '原因代码说明',
                    width : 480
                }]
            });
            domObj.grids.$glassListGrd.jqGrid({
                url : "",
                datatype : "local",
                mtype : "POST",
                height : 350,
                width : domObj.grids.$glassListDiv.width(),
                autowidth : true,
                shrinkToFit:false,
                autoScroll: false,
                resizable : true,
                loadonce : true,
                viewrecords : true,
                rownumbers : true,
                rowNum : 20,
                rownumWidth : 30,
                emptyrecords : true,
                multiselect : false,
                pager : domObj.grids.$glassListPg,
                colModel : [
                    {   name : 'slot_no',
                        index : 'slot_no',
                        label : '子格位',
                        hidden: true,
                        width : 130
                    },
                    {   name : 'prd_seq_id',
                        index : 'prd_seq_id',
                        label : '屏体代码',
                        width : 150
                    },
                    {   name : 'prd_stat',
                        index : 'prd_stat',
                        label : '屏体状态',
                        width : 130
                    },
                    {   name : 'prd_qty',
                        index : 'prd_qty',
                        label : '面板计数',
                        width : 100
                    },
                    {   name : 'mdl_id_fk',
                        index : 'mdl_id_fk',
                        label : '产品代码',
                        width : 150
                    },
                    {   name : 'prd_grade',
                        index : 'prd_grade',
                        label : '屏体等级',
                        width : 100
                    },
                    {   name : 'wo_id_fk',
                        index : 'wo_id_fk',
                        label : '工单代码',
                        width : 150
                    },
                    {   name : 'cus_id_fk',
                        index : 'cus_id_fk',
                        label : '客户代码',
                        hidden: true,
                        width : 150
                    },{ name : 'ext_9',
                        index : 'ext_9',
                        label : '屏体okORng',
                        width : 100,
                    },
                ],
                onSelectRow : function(rowId) {
                    var rowData = domObj.grids.$glassListGrd.jqGrid("getRowData", rowId);
                    var prd_seq_Id = rowData.prd_seq_id;
                    inObj={
                        trx_id : VAL.T_FIPINQSHT,
                        action_flg : "Q",
                        prd_seq_id : prd_seq_Id
                    }
                    outObj = comTrxSubSendPostJson(inObj);
                    if (outObj.rtn_code === VAL.NORMAL) {
                        domObj.$prdSeqId.val(prd_seq_Id);
                        domObj.$prdStat.val(outObj.prd_stat);
                        domObj.$prdGrade.val(outObj.prd_grade);
                        domObj.$mdlIdFk.val(outObj.mdl_id_fk);
                        domObj.$woIdFk.val(outObj.wo_id_fk);
                        domObj.$crOpeNoFk.val(outObj.cr_ope_no_fk);
                        domObj.$nxOpeNoFk.val(outObj.ope_no_fk);
                        domObj.$nxProcIdFk.val(outObj.proc_id_fk);
                        domObj.$nxPathIdFk.val(outObj.path_id_fk);
                        domObj.$nxPathVerFk.val(outObj.path_ver_fk);
                        domObj.$crPathIdFk.val(outObj.cr_path_id_fk);
                        domObj.$crPathVerFk.val(outObj.cr_path_ver_fk);
                    }
                 }
            });
        }
        //模态框grid初始化
        var inModelGrid = function() {
            domObj.dialogs.$modalReasonListGrd
                .jqGrid("clearGridData");
            domObj.dialogs.$reModalReasonListGrd
                .jqGrid("clearGridData");
            domObj.dialogs.$modalReasonListGrd
                .jqGrid({
                    url : "",
                    datatype : "local",
                    mtype : "POST",
                    height : 300,
                    // width :
                    // domObj.dialogs.$modalReasonListDiv.width()*0.99,
                    width : 400,
                    autowidth : true,
                    shrinkToFit : true,
                    resizable : true,
                    loadonce : true,
                    viewrecords : true,
                    rownumbers : true,
                    rowNum : 20,
                    emptyrecords : true,
                    multiselect : false,
                    pager : domObj.dialogs.$modalReasonListPg,
                    colModel : [ {
                        name : 'deptId',
                        index : 'deptId',
                        label : '部门代码',
                        hidden : true,
                        width : 100
                    }, {
                        name : 'data_id',
                        index : 'data_id',
                        label : '原因代码',
                        width : 150
                    }, {
                        name : 'data_desc',
                        index : 'data_desc',
                        label : '原因代码说明',
                        width : 180
                    } ],
                    onSelectRow : function(rowId, status) {
                        var deptId = domObj.dialogs.$deptId.val();
                        if(!deptId){
                            showErrorDialog("", "请选择部门代码");
                            return false;
                        }
                        var rowId= domObj.dialogs.$modalReasonListGrd
                            .jqGrid("getGridParam", "selrow");
                        var rowData = domObj.dialogs.$modalReasonListGrd.jqGrid(
                            "getRowData", rowId);
                        rowData.deptId = deptId;
                        setGridInfo(rowData, "#reModalReasonListGrd");
                    }
                });
            domObj.dialogs.$reModalReasonListGrd.jqGrid({
                url : "",
                datatype : "local",
                mtype : "POST",
                height : 400,
                // width:domObj.dialogs.$reModalReasonListDiv.width()*
                // 0.99,
                width : 400,
                autowidth : true,
                shrinkToFit : true,
                resizable : true,
                loadonce : true,
                viewrecords : true,
                rownumbers : true,
                rowNum : 20,
                emptyrecords : true,
                multiselect  : false,
                pager : domObj.dialogs.$reModalReasonListPg,
                colModel : [ {
                    name : 'deptId',
                    index : 'deptId',
                    label : '部门代码',
                    width : 100
                }, {
                    name : 'data_id',
                    index : 'data_id',
                    label : '原因代码',
                    width : 100
                }, {
                    name : 'data_desc',
                    index : 'data_desc',
                    label : '原因代码说明',
                    width : 100,
                }]
            });
        }
        var dialogFnc = {
            reasonSureFnc : function() {
                var Data = domObj.dialogs.$reModalReasonListGrd.jqGrid("getRowData");
                if(Data.length<1){
                    showErrorDialog("", "请选择一条原因代码");
                    return false;
                }
                setGridInfo(Data, "#reasonListGrd");
                domObj.dialogs.$addReasonDialog.modal("hide");
            },
            updateLevSureFnc : function() {
                var gradeGRP = $.trim(domObj.$gradeGRP.val());
                var prdGrade = $.trim(domObj.$prd_grade.val());
                var ext_9;
                if(domObj.$radioNG.attr("checked")){
                    ext_9 ='NG';
                    var defectCode=$.trim(domObj.$defectCode.val());
                    var prdNote=$.trim(domObj.$prdNote.val());
                    if (!defectCode){
                        showErrorDialog("", "缺陷代码不能为空");
                        return false;
                    }
                    if(!prdNote){
                        showErrorDialog("", "缺陷备注不能为空");
                        return false;
                    }
                    }else{
                    ext_9 ='OK';
                }

                 if(!gradeGRP){
                    showErrorDialog("", "等级类别不能为空");
                    return false;
                 }
                if(!prdGrade){
                    showErrorDialog("", "屏体等级不能为空");
                    return false;
                 }
                var rowId= domObj.grids.$glassListGrd.jqGrid("getGridParam", "selrow");
                var rowData = domObj.grids.$glassListGrd.jqGrid("getRowData", rowId);
                      rowData.prd_grade = prdGrade;
                      rowData.ext_9 = ext_9;
                domObj.grids.$glassListGrd.jqGrid("setRowData",rowId, rowData);
                domObj.dialogs.$updateLevDialog.modal("hide");
            }
          };
        var iniButtonAction = function() {
            domObj.buttons.$query_btn.click(function() {
                btnFunc.queryBtnfunc();
            });
            domObj.buttons.$selectReason_btn.click(function() {
                btnFunc.selectReasonBtnfunc();
            });
            domObj.buttons.$deleteReason_btn.click(function() {
                btnFunc.deleteReasonBtnfunc();
            });
            domObj.buttons.$updateLev_btn.click(function() {
                btnFunc.updateLevBtnfunc();
            });
            domObj.buttons.$save_btn.click(function() {
                btnFunc.saveBtnfunc();
            });
            // 确认按钮绑定click事件函数
            domObj.dialogs.$reasonDialog_sureBtn.bind('click', dialogFnc.reasonSureFnc);
            // 确认按钮绑定click事件函数
            domObj.dialogs.$updateLevDialog_sureBtn.bind('click', dialogFnc.updateLevSureFnc);
            domObj.$radioNG.bind('click', btnFunc.changeNgFnc);
            domObj.$radioOK.bind('click', btnFunc.changeOkFnc);
        };
        var clearDataAction = function() {
            domObj.$prdSeqId.val("");
            domObj.$prdStat.val("");
            domObj.$prdGrade.val("");
            domObj.$mdlIdFk.val("");
            domObj.$woIdFk.val("");
            domObj.$crOpeNoFk.val("");
            domObj.$nxOpeNoFk.val("");
            domObj.$nxProcIdFk.val("");
            domObj.$nxPathIdFk.val("");
            domObj.$nxPathVerFk.val("");
            domObj.$crPathIdFk.val("");
            domObj.$crPathVerFk.val("");
            $("#reasonListGrd").jqGrid('clearGridData');
            $("#glassListGrd").jqGrid('clearGridData');
            $("#modalReasonListDiv").jqGrid('clearGridData');
            $("#reModalReasonListDiv").jqGrid('clearGridData');
            /*   $("#rePathListDiv").jqGrid('clearGridData');*/

        }
        var otherActionBind = function(){
            $("form").submit(function(){
                return false;
            });

        };
        domObj.$prdIdFk.keydown(function(event) {
            if (event.which == 13) {
                btnFunc.queryBtnfunc();
            }
        });
        function initFunc() {
            $(".input").val("");
            iniGridInfo();
            iniButtonAction();
            inModelGrid();// 初始化dailog中的grids
            otherActionBind();
            initSelectFnc.inidefectCode();
        }
        initFunc();
        //enter
        /*document.onkeydown=function(event){
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if(e && e.keyCode==13){ // 按 enter
                $('.bootbox').modal("hide");
            }
        }*/
        //表格自适应
        function resizeFnc(){
            var offsetBottom, grdDivWidth,offsetBottom1, grdDivWidth1,offsetBottom2, grdDivWidth2,offsetBottom3, grdDivWidth3;
            //1
            grdDivWidth = $("#table1Div").width();
            offsetBottom =  $("#reasonListDiv").offset().top;

            $("#reasonListDiv").width(grdDivWidth*0.98);
            $("#reasonListDiv").height(offsetBottom*0.99);
            $("#reasonListGrd").setGridWidth(grdDivWidth*0.97);
            $("#reasonListGrd").setGridHeight(offsetBottom*0.95-51);
            //2
            grdDivWidth1 = $("#table2Div").width();
            offsetBottom1 = $(window).height() - $("#glassListDiv").offset().top;

            $("#glassListDiv").width(grdDivWidth1*0.98);
            $("#glassListDiv").height(offsetBottom1*0.99);
            $("#glassListGrd").setGridWidth(grdDivWidth1*0.97);
            $("#glassListGrd").setGridHeight(offsetBottom1*0.95-51);
        };
        resizeFnc();
        $(window).resize(function() {
            resizeFnc();
        });

    })